package com.testing.common;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;

/**
 * @Classname RobotUtils
 * @Description 类型说明
 * @Date 2022/10/12 21:40
 * @Created by 特斯汀Roy
 */
public class RobotUtils {

    /**
     * 鼠标移动到指定坐标点击。
     * @param x
     * @param y
     */
    public static void rbClick(int x,int y){
        try {
            Robot robot=new Robot();
            robot.mouseMove(x,y);
            robot.mousePress(KeyEvent.BUTTON1_MASK);
            robot.mouseRelease(KeyEvent.BUTTON1_MASK);
        } catch (Exception e) {
            e.printStackTrace();
            AutoTools.log.error("用robot控制鼠标点击失败",e.fillInStackTrace());
        }
    }

    /**
     * 通过robot借助剪贴板完成输入
     * @param text
     */
    public static void rbInput(String text){
        try {
            Robot robot=new Robot();
            //开启一个剪贴板
            Clipboard clipboard=Toolkit.getDefaultToolkit().getSystemClipboard();
            //剪贴的内容
            StringSelection content=new StringSelection(text);
            clipboard.setContents(content,null);
            //Robot实现粘贴
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_CONTROL);
            robot.keyRelease(KeyEvent.VK_V);
        } catch (Exception e) {
            e.printStackTrace();
            AutoTools.log.error("输入指定内容"+text+"失败",e.fillInStackTrace());
        }
    }

    /**
     * 使用robot完成回车操作
     */
    public static void rbEnter(){
        try {
            Robot robot=new Robot();
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);
        } catch (AWTException e) {
            e.printStackTrace();
            AutoTools.log.error("回车失败",e.fillInStackTrace());
        }
    }
}
